package com.portme.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.DAOBase;
import com.portme.server.pojos.CustomTab;
import com.portme.server.pojos.HomePage;
import com.portme.server.pojos.PUser;
import com.portme.server.pojos.Post;
import com.portme.server.pojos.Profile;

public class DAO extends DAOBase{
	static {
		ObjectifyService.register(Profile.class);
		ObjectifyService.register(PUser.class);
		ObjectifyService.register(CustomTab.class);
		ObjectifyService.register(Post.class);
		ObjectifyService.register(HomePage.class);
	}
	
	public Profile savePuser(String username) {
		PUser puser = null;
		try {
			puser = ofy().get(PUser.class, username);
		} catch (NotFoundException e) {
			Profile profile = null;
			try {
				puser = new PUser(username);
				profile = new Profile(username);
				Key<Profile> profileKey = ofy().put(profile);
				HomePage homePage = new HomePage(username);
				Key<HomePage> homePageKey = ofy().put(homePage);
				profile.setHomePage(homePageKey.getId());
				puser.setProfile(profileKey.getId());
				ofy().put(profile);
				ofy().put(puser);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			return profile;
		}
		if(puser != null)
			return ofy().get(Profile.class, puser.getProfile());
		else 
			return null;
	}
	
	public Profile getProfile(String user) {
		PUser puser = null;
		try {
			puser = ofy().get(PUser.class, user);
		} catch (NotFoundException e) {}
		if(puser != null)
			return ofy().get(Profile.class, puser.getProfile());
		else 
			return null;
	}
	
	//send profile id instead of username ...  this is pathetic coding
	public CustomTab createTab(String name, String user) {
		CustomTab tab = new CustomTab(name);
		Key<CustomTab> tabKey = ofy().put(tab);
		
		PUser puser = ofy().get(PUser.class, user);
		Profile profile = ofy().get(Profile.class, puser.getProfile());
		profile.addCustomTab(tabKey.getId());
		ofy().put(profile);
		
		return ofy().get(CustomTab.class, tabKey.getId());
	}
	
	public Map<Long, CustomTab> getAllTabs(List<Long> tabIds) {
		return ofy().get(CustomTab.class, tabIds);
	}
	
	public Post createPost(Long tab_id, String title, String text) {
		Post newPost = new Post(title, text);
		Key<Post> postKey = ofy().put(newPost);
		
		try {
			CustomTab tab = ofy().get(CustomTab.class, tab_id);
			tab.addPost(postKey.getId());
			ofy().put(tab);
		} catch (NotFoundException e) {
			return null;
		}
		return ofy().get(Post.class, postKey.getId());
	}
	
	public Post editPost(Long post_id, String title, String text) {
		try {
			Post post = ofy().get(Post.class, post_id);
			post.setText(text);
			post.setTitle(title);
			ofy().put(post);
			return ofy().get(Post.class, post_id);
		} catch (NotFoundException e) {
			return null;
		}
	}
	
	public boolean deletePost(Long post_id) {
		try {
			ofy().delete(Post.class, post_id);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public Map<Long, Post> getAllPostsInTab(List<Long> postIds) {
		if(postIds != null)
			return ofy().get(Post.class, postIds);
		else 
			return new HashMap<Long, Post>();
	}
	
	public Post getPost(Long postId)
	{
		if(postId != null)
			return ofy().get(Post.class, postId);
		return null;
	}
	
	public String setTheme(String theme, String user)
	{
		PUser puser = ofy().get(PUser.class, user);
		Profile profile = ofy().get(Profile.class, puser.getProfile());
		profile.setTheme(theme);
		ofy().put(profile);
		return ofy().get(Profile.class, puser.getProfile()).getTheme();
	}

	public HomePage setHomePage(String siteTitle, String pitchText, String about, String contact,
			String user) {
		try{
			PUser puser = ofy().get(PUser.class, user);
			Profile profile = ofy().get(Profile.class, puser.getProfile());
			HomePage homePage = ofy().get(HomePage.class, profile.getHomePage());
			homePage.setHomePage(siteTitle, pitchText, about, contact);
			return homePage;
		}
		catch(NotFoundException ex)
		{
			return null;
		}
	}

	public boolean addPostToHomePage(Long postId, String user) {
		try{
			PUser puser = ofy().get(PUser.class, user);
			Profile profile = ofy().get(Profile.class, puser.getProfile());
			HomePage homePage = ofy().get(HomePage.class, profile.getHomePage());
			homePage.addPostToHomePage(postId);
			return true;
		}
		catch(NotFoundException ex)
		{
			return false;
		}
		
		
	}
	public HomePage getHomePage(Long homePage) {
		if(homePage != null)
		{
			return ofy().get(HomePage.class, homePage);
		}
		return null;
	}
	
}
